import Storage from '@/utils/storage';

export type ICommodityCache = {
  id: number;
  name: string;
  avatar: string;
};
/**
 * 增加阿姨浏览记录缓存
 * @param commodity Resume
 */
export async function addHistoryCache({ id, name, avatar }: ICommodityCache): Promise<void> {
  const cacheList: ICommodityCache[] =
    JSON.parse((await Storage.getItem('commodity')) || '[]') || [];
  const index = cacheList.findIndex((v) => v.id === id);
  if (index > -1) {
    cacheList.splice(index, 1);
  }
  cacheList.unshift({
    id,
    name,
    avatar,
  });

  Storage.setItem('commodity', JSON.stringify(cacheList.slice(0, 10)));
}

/**
 * 获取阿姨浏览记录缓存
 * @returns commodity ICommodityCache
 */
export async function getHistoryCache(): Promise<ICommodityCache[]> {
  const cacheList: ICommodityCache[] =
    JSON.parse((await Storage.getItem('commodity')) || '[]') || [];
  return cacheList;
}
